﻿using Colorful.Furniture.Enum;
using Microsoft.Extensions.Logging;
using System;
using Volo.Abp.BackgroundJobs;
using Volo.Abp.DependencyInjection;
using FreeSql;
using Colorful.Furniture.BasicDataManagement;
using Microsoft.Extensions.Options;

namespace Colorful.Furniture.BackgroundManagement;

/// <summary>
/// 修改订单状态-后台作业
/// </summary>
public class UpdateOrderStatusJob : BackgroundJob<UpdateOrderStatusArgs>, ISingletonDependency
{
    private readonly IFreeSql _freeSql;
    private readonly ILogger<UpdateOrderStatusArgs> _logger;
    //private readonly IBaseRepository<CustomerOrderCallbackLog> _customerOrderCallbackLogRepository;
    private readonly IBackgroundJobManager _backgroundJobManager;
    private readonly FileConfig _fileConfig;

    public UpdateOrderStatusJob(
        IBackgroundJobManager backgroundJobManager,
        IFreeSql freeSql,
        ILogger<UpdateOrderStatusArgs> logger,
        //IBaseRepository<CustomerOrderCallbackLog> customerOrderCallbackLogRepository,
        IOptions<FileConfig> fileConfig
    )
    {
        _backgroundJobManager = backgroundJobManager;
        _freeSql = freeSql;
        _logger = logger;
        //_customerOrderCallbackLogRepository = customerOrderCallbackLogRepository;
        _fileConfig = fileConfig.Value;
    }


    /// <summary>
    /// 执行修改订单状态
    /// </summary>
    /// <param name="args"></param>
    public override async void Execute(UpdateOrderStatusArgs args)
    {
        try
        {
            if (args.Status == EnumOrderStatus.充值失败)
            {
                _logger.LogWarning($"【修改订单状态】-【完成】，订单号：{args.OrderDownNo}");

                ////修改订单状态信息
                //await _freeSql.Update<CustomerOrder>()
                //    .Set(x => x.Status, EnumOrderStatus.订单完成)
                //    .Set(x => x.PaymentStatus, EnumPaymentStatus.支付成功)
                //    .Set(x => x.FinishTime, DateTime.Now)
                //    .Set(x => x.OrderStatus, 2)
                //    .Set(x => x.Remark, args.Message ?? "充值成功")
                //    .SetIf(!string.IsNullOrWhiteSpace(args.ThirdSeri), x => x.ThirdSeri == args.ThirdSeri)
                //    .SetIf(!string.IsNullOrWhiteSpace(args.SupplierOrderNo), x => x.SupplierOrderNo == args.SupplierOrderNo)
                //    .Where(x => x.OrderDownNo == args.OrderDownNo)
                //    .ExecuteAffrowsAsync();

                ////修改客户订单日志
                //await _freeSql.Update<CustomerOrderLog>()
                //    .Set(x => x.Remark, $"充值成功：{args.Message}")
                //    .Where(x => x.OrderDownNo == args.OrderDownNo && x.SupplierId == args.SupplierId)
                //    .ExecuteAffrowsAsync();

            }
            else if (args.Status == EnumOrderStatus.充值失败)
            {
                _logger.LogWarning($"【修改订单状态】-【失败】，订单号：{args.OrderDownNo}");

                ////修改订单状态信息
                //await _freeSql.Update<CustomerOrder>()
                //    .Set(x => x.Status, EnumOrderStatus.订单失败)
                //    .Set(x => x.PaymentStatus, EnumPaymentStatus.支付失败)
                //    .Set(x => x.FinishTime, DateTime.Now)
                //    .Set(x => x.SupplierOrderNo, args.SupplierOrderNo)
                //    .Set(x => x.OrderStatus, 2)
                //    .Set(x => x.Remark, args.Message ?? "充值失败")
                //    .Where(x => x.OrderDownNo == args.OrderDownNo)
                //    .ExecuteAffrowsAsync();

                ////修改客户订单日志
                //await _freeSql.Update<CustomerOrderLog>()
                //    .Set(x => x.Remark, $"充值失败：{args.Message}")
                //    .Where(x => x.OrderDownNo == args.OrderDownNo && x.SupplierId == args.SupplierId)
                //    .ExecuteAffrowsAsync();
            }
            else if (args.Status == EnumOrderStatus.存疑)
            {
                //_logger.LogWarning($"【修改订单状态】-【存疑】，订单号：{args.OrderDownNo}");

                ////修改订单状态信息
                //await _freeSql.Update<CustomerOrder>()
                //    .Set(x => x.Status, EnumOrderStatus.存疑)
                //    .Set(x => x.Remark, $"订单存疑：{args.Message}")
                //    .Set(x => x.SupplierOrderNo, args.SupplierOrderNo)
                //    .Set(x => x.OrderStatus, 1)
                //    .Where(x => x.OrderDownNo == args.OrderDownNo)
                //    .ExecuteAffrowsAsync();

                ////修改客户订单日志
                //await _freeSql.Update<CustomerOrderLog>()
                //    .Set(x => x.Remark, "订单存疑，需人工核实原因：" + args.Message)
                //    .Where(x => x.OrderDownNo == args.OrderDownNo && x.SupplierId == args.SupplierId)
                //    .ExecuteAffrowsAsync();
            }
            
        }
        catch (Exception e)
        {
            _logger.LogWarning($"继续执行【结束-修改订单状态-错误】，订单号：{args.OrderDownNo}，错误原因：{e.Message}");
        }
    }

}


public class UpdateOrderStatusArgs
{
    /// <summary>
    /// 订单类型：1 = 充值订单； 2 = 存量业务； 3 = 号卡业务；
    /// </summary>
    public int OrderType { get; set; }

    /// <summary>
    /// 平台订单号
    /// </summary>
    public string OrderDownNo { get; set; }

    /// <summary>
    /// 订单状态
    /// </summary>
    public EnumOrderStatus Status { get; set; }

    /// <summary>
    /// 订单描述
    /// </summary>
    public string Message { get; set; }

    /// <summary>
    /// 供应商编号
    /// </summary>
    public Guid? SupplierId { get; set; }

    /// <summary>
    /// 供应商订单号
    /// </summary>
    public string SupplierOrderNo { get; set; }

    /// <summary>
    /// 透传流水号（官方）
    /// </summary>
    public string ThirdSeri { get; set; }

}
